package com.shrct.businessprocess.product.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.organization.RCEnterprise;
import com.shrct.businessprocess.organization.RCOrganizationService;
import com.shrct.businessprocess.organization.RCSupplier;
import com.shrct.businessprocess.product.RCAnonymousProductEntry;
import com.shrct.businessprocess.product.RCAnonymousProductEntryDAO;
import com.shrct.businessprocess.product.RCAnonymouseProductEntryRoot;
import com.shrct.businessprocess.product.RCProductSpecification;
import com.shrct.businessprocess.product.RCRegisterAnonymousProductEvent;
import com.shrct.businessprocess.product.RCUnregisterAnonymousProductEvent;
import com.shrct.businessprocess.product.impl.RCAnonymouseProductEntryRootImpl;
import com.shrct.businessprocess.product.impl.RCRegisterAnonymousProductEventImpl;
import com.shrct.businessprocess.product.impl.RCUnregisterAnonymousProductEventImpl;
import com.shrct.businessprocess.purchase.RCAnonymousProductInStockEvent;
import com.shrct.businessprocess.purchase.RCPurchaseOrder;
import com.shrct.businessprocess.purchase.RCPurchaseOrderItem;
import com.shrct.businessprocess.purchase.impl.RCAnonymousProductInStockEventImpl;
import edu.fudan.langlab.domain.security.ISystemUserService;
import edu.fudan.langlab.domain.security.User;
import edu.fudan.mylang.pf.BaseDAOService;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.utils.DateUtil;
import java.util.Date;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("com.shrct.businessprocess.product.RCAnonymousProductEntryDAO")
public class RCAnonymousProductEntryDAOImpl extends BaseDAOService<RCAnonymousProductEntry> implements RCAnonymousProductEntryDAO {
  @Autowired
  private ISystemUserService systemUserService;
  
  @Autowired
  private RCOrganizationService organizationService;
  
  public RCAnonymousProductEntry create(final String batchSid, final Procedure1<? super RCAnonymousProductEntry> postInit) {
    RCAnonymousProductEntry _xblockexpression = null;
    {
      IGenericQuery<RCAnonymouseProductEntryRoot> query=getObjectFactory().createGenericQuery(RCAnonymouseProductEntryRoot.class,"select e from com.shrct.businessprocess.product.impl.RCAnonymouseProductEntryRootImpl as e   where ((e.batchSid) = (:batchSid)) and  e.active = 1      ").setParameter("batchSid",batchSid);
      RCAnonymouseProductEntryRoot _entryRoot = query.uniqueResult();
      RCAnonymouseProductEntryRoot _xifexpression = null;
      boolean _equals = Objects.equal(_entryRoot, null);
      if (_equals) {RCAnonymouseProductEntryRoot rCAnonymouseProductEntryRoot=RCAnonymouseProductEntryRootImpl.create(getObjectFactory());
        final Procedure1<RCAnonymouseProductEntryRoot> _function = new Procedure1<RCAnonymouseProductEntryRoot>() {
            public void apply(final RCAnonymouseProductEntryRoot root) {
              root.setBatchSid(batchSid);
              root.start();
            }
          };
        RCAnonymouseProductEntryRoot _doubleArrow = ObjectExtensions.<RCAnonymouseProductEntryRoot>operator_doubleArrow(rCAnonymouseProductEntryRoot, _function);
        _xifexpression = _doubleArrow;
      } else {
        _xifexpression = _entryRoot;
      }
      final RCAnonymouseProductEntryRoot entryRoot = _xifexpression;
      RCAnonymousProductEntry _createAndAddtoEntries = entryRoot.createAndAddtoEntries();
      final Procedure1<RCAnonymousProductEntry> _function_1 = new Procedure1<RCAnonymousProductEntry>() {
          public void apply(final RCAnonymousProductEntry it) {
            it.setEntryRoot(entryRoot);
            postInit.apply(it);
          }
        };
      final RCAnonymousProductEntry entry = ObjectExtensions.<RCAnonymousProductEntry>operator_doubleArrow(_createAndAddtoEntries, _function_1);
      _xblockexpression = (entry);
    }
    return _xblockexpression;
  }
  
  public RCAnonymousProductEntry registerAnonymousProduct(final String projectId, final RCEnterprise enterprise, final String sid, final RCProductSpecification type, final Integer count) {
    RCAnonymousProductEntry _xblockexpression = null;
    {
      IGenericQuery<RCAnonymousProductEntry> query=getObjectFactory().createGenericQuery(RCAnonymousProductEntry.class,"select product from com.shrct.businessprocess.product.impl.RCAnonymousProductEntryImpl as product   where (((product.enterprise) = (:enterprise)) and ((product.sid) = (:sid))) and  product.active = 1      ").setParameter("enterprise",enterprise).setParameter("sid",sid);
      RCAnonymousProductEntry product = query.uniqueResult();
      boolean _equals = Objects.equal(product, null);
      if (_equals) {
        final Procedure1<RCAnonymousProductEntry> _function = new Procedure1<RCAnonymousProductEntry>() {
            public void apply(final RCAnonymousProductEntry it) {
              it.setProjectId(projectId);
              it.setSid(sid);
              it.setEnterprise(enterprise);
              it.setType(type);
              it.setCount(count);
            }
          };
        RCAnonymousProductEntry _create = this.create(sid, _function);
        product = _create;
      } else {
        Integer _count = product.getCount();
        int _plus = ((_count).intValue() + (count).intValue());
        product.setCount(Integer.valueOf(_plus));
      }RCRegisterAnonymousProductEvent rCRegisterAnonymousProductEvent=RCRegisterAnonymousProductEventImpl.create(product,count,getObjectFactory());
      final Procedure1<RCRegisterAnonymousProductEvent> _function_1 = new Procedure1<RCRegisterAnonymousProductEvent>() {
          public void apply(final RCRegisterAnonymousProductEvent it) {
            Date _day = DateUtil.today();
            it.setDate(_day);
            User _currentUser = RCAnonymousProductEntryDAOImpl.this.systemUserService.getCurrentUser();
            it.setOperator(_currentUser);
          }
        };
      ObjectExtensions.<RCRegisterAnonymousProductEvent>operator_doubleArrow(rCRegisterAnonymousProductEvent, _function_1);
      _xblockexpression = (product);
    }
    return _xblockexpression;
  }
  
  public String calcAnonymoseProductSid(final RCPurchaseOrderItem orderItem) {
    RCProductSpecification _productSpec = orderItem.getProductSpec();
    String _code = _productSpec.getCode();
    String _plus = (_code + "$");
    RCPurchaseOrder _purchaseOrder = orderItem.getPurchaseOrder();
    Long _id = _purchaseOrder.getId();
    String _plus_1 = (_plus + _id);
    return _plus_1;
  }
  
  /**
   * 产品入库，登记产品并将其入库至云多的库存
   */
  public RCAnonymousProductEntry inStockAnonymousProduct(final String projectId, final RCPurchaseOrderItem orderItem, final Integer count) {
    RCEnterprise _hostEnterprise = this.organizationService.getHostEnterprise();
    String _calcAnonymoseProductSid = this.calcAnonymoseProductSid(orderItem);
    RCProductSpecification _productSpec = orderItem.getProductSpec();
    RCAnonymousProductEntry _registerAnonymousProduct = this.registerAnonymousProduct(projectId, _hostEnterprise, _calcAnonymoseProductSid, _productSpec, count);
    final Procedure1<RCAnonymousProductEntry> _function = new Procedure1<RCAnonymousProductEntry>() {
        public void apply(final RCAnonymousProductEntry product) {
          RCPurchaseOrder _purchaseOrder = orderItem.getPurchaseOrder();
          RCSupplier _supplier = _purchaseOrder.getSupplier();
          product.setSupplier(_supplier);
          RCAnonymouseProductEntryRoot _entryRoot = product.getEntryRoot();
          _entryRoot._inStock(orderItem, count);
          RCAnonymouseProductEntryRoot _entryRoot_1 = product.getEntryRoot();RCAnonymousProductInStockEvent rCAnonymousProductInStockEvent=RCAnonymousProductInStockEventImpl.create(_entryRoot_1,getObjectFactory());
          final Procedure1<RCAnonymousProductInStockEvent> _function = new Procedure1<RCAnonymousProductInStockEvent>() {
              public void apply(final RCAnonymousProductInStockEvent it) {
                it.setOrderItem(orderItem);
                Date _day = DateUtil.today();
                it.setDate(_day);
                it.setProduct(product);
                it.setQuantity(count);
              }
            };
          ObjectExtensions.<RCAnonymousProductInStockEvent>operator_doubleArrow(rCAnonymousProductInStockEvent, _function);
        }
      };
    RCAnonymousProductEntry _doubleArrow = ObjectExtensions.<RCAnonymousProductEntry>operator_doubleArrow(_registerAnonymousProduct, _function);
    return _doubleArrow;
  }
  
  public void transferAnonymousProduct(final String projectId, final String sid, final Integer count, final RCEnterprise enterprise) {
    RCEnterprise hostEnterprise = this.organizationService.getHostEnterprise();
    IGenericQuery<RCAnonymousProductEntry> query=getObjectFactory().createGenericQuery(RCAnonymousProductEntry.class,"select product from com.shrct.businessprocess.product.impl.RCAnonymousProductEntryImpl as product   where (((product.sid) = (:sid)) and ((product.enterprise) = (:hostEnterprise))) and  product.active = 1      ").setParameter("sid",sid).setParameter("hostEnterprise",hostEnterprise);
    final RCAnonymousProductEntry product = query.uniqueResult();RCUnregisterAnonymousProductEvent rCUnregisterAnonymousProductEvent=RCUnregisterAnonymousProductEventImpl.create(product,count,getObjectFactory());
    final Procedure1<RCUnregisterAnonymousProductEvent> _function = new Procedure1<RCUnregisterAnonymousProductEvent>() {
        public void apply(final RCUnregisterAnonymousProductEvent it) {
          Date _day = DateUtil.today();
          it.setDate(_day);
          User _currentUser = RCAnonymousProductEntryDAOImpl.this.systemUserService.getCurrentUser();
          it.setOperator(_currentUser);
        }
      };
    ObjectExtensions.<RCUnregisterAnonymousProductEvent>operator_doubleArrow(rCUnregisterAnonymousProductEvent, _function);
    Integer _count = product.getCount();
    int _minus = ((_count).intValue() - (count).intValue());
    product.setCount(Integer.valueOf(_minus));
    String _sid = product.getSid();
    RCProductSpecification _type = product.getType();
    RCAnonymousProductEntry _registerAnonymousProduct = this.registerAnonymousProduct(projectId, enterprise, _sid, _type, count);
    final Procedure1<RCAnonymousProductEntry> _function_1 = new Procedure1<RCAnonymousProductEntry>() {
        public void apply(final RCAnonymousProductEntry it) {
          RCAnonymouseProductEntryRoot _entryRoot = product.getEntryRoot();
          _entryRoot._deliver(it, count);
        }
      };
    ObjectExtensions.<RCAnonymousProductEntry>operator_doubleArrow(_registerAnonymousProduct, _function_1);
  }
}
